﻿
namespace Storyteller.Framework.Graphics.Particles.Modifiers
{
	public class ColorInterpolatorModifier : ParticleModifier
	{
		public Color InitialColor { get; set; }
		public Color MiddleColor { get; set; }
		public Color FinalColor { get; set; }

		public float MiddlePosition { get; set; }

		public override void Process(float deltaSeconds, Particle[] particles, int count)
		{
			for (int i = 0; i < count; i++)
			{
				Particle p = particles[i];

				if (p.Lifetime < MiddlePosition)
				{
					p.Color.R = InitialColor.R + ((MiddleColor.R - InitialColor.R) * (p.Lifetime / MiddlePosition));
					p.Color.G = InitialColor.G + ((MiddleColor.G - InitialColor.G) * (p.Lifetime / MiddlePosition));
					p.Color.B = InitialColor.B + ((MiddleColor.B - InitialColor.B) * (p.Lifetime / MiddlePosition));
				}
				else
				{
					p.Color.R = MiddleColor.R + ((FinalColor.R - MiddleColor.R) * ((p.Lifetime - MiddlePosition) / (1.0f - MiddlePosition)));
					p.Color.G = MiddleColor.G + ((FinalColor.G - MiddleColor.G) * ((p.Lifetime - MiddlePosition) / (1.0f - MiddlePosition)));
					p.Color.B = MiddleColor.B + ((FinalColor.B - MiddleColor.B) * ((p.Lifetime - MiddlePosition) / (1.0f - MiddlePosition)));
				}
			}
		}
	}
}
